<?php
namespace cms\modules\v1\controllers;

use cms\modules\v1\models\access\ModuleActions;
use cms\modules\v1\models\admin\Admin;
use cms\modules\v1\models\admin\AdminLoginLog;
use cms\modules\v1\models\admin\AdminRole;
use common\models\Functions;
use Yii;
use yii\filters\auth\HttpHeaderAuth;
use yii\helpers\ArrayHelper;

class AdminController extends DefaultController
{
    public $modelClass = 'cms\modules\v1\models\admin\Admin';

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            'authenticator' => [
                'class' =>  HttpHeaderAuth::className(),    //head头部验证
                'header' => 'access-token',
                'optional' => [                     //需要过滤掉不用token参数的方法
                ]
            ]
        ]);
    }
    /* ======================  管理员设置 ========================= */

    //列表信息显示

    public function actionList($page = 1, $size = 20, $search = ''){
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }

        if (Yii::$app->request->isGet) {
            return  (new Admin())->getListData($page, $size, $search);
        }
    }

    public function actionMfa() {
        if (Yii::$app->request->isGet) {
            return (new Admin())->getMfa();
        }

        if (Yii::$app->request->isPost) {
            return (new Admin())->bindMfa();
        }
        if (Yii::$app->request->isDelete) {
            return (new Admin())->unbindMfa();
        }
    }

    //切换启用禁用状态
    public function actionStatus()
    {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }


        if (Yii::$app->request->isPost) {
            return (new Admin())->switchStatusAdmin();
        }
    }

    //修改密码
    public function actionInfo($id = 0)
    {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }

        if (Yii::$app->request->isPost) {
            return (new Admin())->saveInfo();
        }

        if (Yii::$app->request->isGet) {
            return (new Admin())->getEditInfo($id);
        }
    }

    /**
     * 获取管理员信息
     * @return array
     */
    public function actionAdminInfo()
    {
//        $check = (new ModuleActions())->checkActions();
//        if ($check !== true) {
//            return Functions::formatRestAPI(1, $check);
//        }

        if (Yii::$app->request->isGet) {
            return (new Admin())->getBaseInfo();
        }
    }

    /* ======================  角色设置 ========================= */
    /**
     * 获取角色MAP
     * @return array
     */
    public function actionRoleMap() {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }

        if (Yii::$app->request->isGet) {
            return (new AdminRole())->getMapForApi();
        }
    }

    public function actionRoleList() {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }

        if (Yii::$app->request->isGet) {
            return (new AdminRole())->getListData();
        }
    }

    public function actionRoleInfo($id = 0) {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }

        if (Yii::$app->request->isGet) {
            return (new AdminRole())->getInfo($id);
        }
        if (Yii::$app->request->isPost) {
            return (new AdminRole())->saveInfo();
        }
    }

    public function actionRoleTree($id = 0) {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }

        if (Yii::$app->request->isGet) {
            return (new AdminRole())->getTree($id);
        }
    }
    public function actionRoleStatus() {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }

        if (Yii::$app->request->isPost) {
            return (new AdminRole())->switchStatus();
        }
    }

    /* ======================  登录日志设置 ========================= */
    public function actionLoginLog($page = 1, $size = 50) {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }

        if (Yii::$app->request->isGet) {
            return (new AdminLoginLog())->getList($page, $size);
        }
    }
}
